function longestPalindrome(s) {
  const n = s.length
  const dp = []
  for(let i = 0; i < n; i++) dp.push([])
  let ans = ''
  for(let l = 0; l < n; ++l) {
    for(let i = 0; i + 1 < n; ++i) {
      const j = i + l
      if (l === 0) {
        dp[i][j] = true
      } else if (l === 1) {
        dp[i][j] = s[i] === s[j]
      } else {
        dp[i][j] = s[i] === s[j] && dp[i + 1][j - 1]
      }
      if (dp[i][j] && l + 1 > ans.length) ans = s.substring(i, j + 1)
    }
  }
  return ans
}

longestPalindrome('babad')
